package dk.zpon.andnoise;

import org.json.JSONException;
import org.json.JSONObject;

import dk.zpon.andnoise.R;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class PlaylistAdapter extends BaseAdapter {

	private Connection mCon;
	private LayoutInflater mInflater;
	private JSONObject mData[];
	private int mTotal;
	private int mCount;
	private Context mContext;
	private boolean mBusy = false;

	public PlaylistAdapter(Context context)
	{
		mInflater = LayoutInflater.from(context);
		mCon = Connection.getInstance();
		mTotal = mCon.playlistGetLength();
		mCount = mTotal;
		mData = new JSONObject[mCount];
	}

	public void setBusy(boolean b)
	{
		this.mBusy = b;
	}

	public void removeItem(int position)
	{
		mData[position] = null;
		mCount--;
	}

	public void clear()
	{
		mData = new JSONObject[mCount];
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mCount;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) 
	{	
		EntryHolder holder;
		JSONObject data = null;

		if(this.mData[position] != null)
		{
			int i = -1;
			int y = -1;
			while(i != position)
			{
				y++;
				if(mData[y] != null)
					i++;
			}
			data = this.mData[y];
		}
		if(convertView == null)
		{
			convertView = mInflater.inflate(R.layout.playlist_entry, null);

			if(data == null && !mBusy)
			{
				data = mCon.playlistGetInfo(position);
				mData[position] = data;
			}

			if(data == null)
			{
				holder = new EntryHolder(position, null, convertView);
			}
			else
			{
				holder = new EntryHolder(position, data, convertView);
			}
		}
		else
		{
			holder = (EntryHolder) convertView.getTag();

			if(this.mBusy)
				holder.setData(null);
			else
				holder.setData(mCon.playlistGetInfo(position));

			holder.setPosition(position);
			holder.setText();
		}


		convertView.setTag(holder);

		return convertView;
	}
}

class EntryHolder 
{
	private TextView mNumber;
	private TextView mTitle;
	private TextView mArtist;
	private TextView mAlbum;
	private PlaylistView mPlaylistView;
	private int mPosition;
	private JSONObject mData;
	private View mView;
	private boolean mDeleted;

	public EntryHolder(int position, JSONObject data, View view)
	{
		/*mNumber = (TextView) view.findViewById(R.id.playlist_number);
		mTitle = (TextView) view.findViewById(R.id.playlist_title);
		mArtist = (TextView) view.findViewById(R.id.playlist_artist);
		mAlbum = (TextView) view.findViewById(R.id.playlist_album);
		*/
		mPlaylistView = (PlaylistView) view.findViewById(R.id.playlist_entry);
		mPosition = position;
		mData = data;
		mView = view;
		mDeleted = false;

		this.setText();
	}

	public void setPosition(int position) {
		this.mPosition = position;
	}

	public boolean dataIsSet()
	{
		return mData != null;
	}

	public void setData(JSONObject data)
	{
		this.mData = data;
	}

	public void setText()
	{
		//mNumber.setText(Integer.toString(mPosition));

		if(mData != null)
		{
			try {
				String title = mData.getString("TITLE");
				String artist = mData.getString("ARTIST");
				String album = mData.getString("ALBUM");
				String path = mData.getString("PATH");
				
				int i = path.lastIndexOf("/");
				if(i > -1)
					path = path.substring(i+1);
				
				mPlaylistView.setData((title.equals("") ? path : title), (artist.length() == 0 ? "?" : artist), (album.length() == 0 ? "?" : album));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			mPlaylistView.setData("Loading...", "?", "?");
		}
	}

	public int getPosition()
	{
		return mPosition;
	}

	public View getView()
	{
		return this.mView;
	}

	public void delete()
	{
		mDeleted = true;
	}

	public boolean isDeleted()
	{
		return mDeleted;
	}
}